#!/bin/sh
#
# Copyright 2021 The Modelbox Project Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

### BEGIN INIT INFO
# Provides:        modelbox
# Required-Start:  $network 
# Required-Stop:   $network 
# Default-Start:   2 3 4 5
# Default-Stop:
# Short-Description: Start modelbox service
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

MODELBOX_ROOT=$(cd $(dirname $0)/../../ && pwd)
if [ "$MODELBOX_ROOT" = "/" ]; then
	MODELBOX_ROOT=""
fi

# this env may changed by script, do not modify
CUSTOM_ENV_FILE=""

if [ -f "$CUSTOM_ENV_FILE" ]; then
	. $CUSTOM_ENV_FILE
else
	. ${MODELBOX_ROOT}@CMAKE_INSTALL_FULL_SYSCONFDIR@/modelbox/modelbox-opts
	PIDDIR=${MODELBOX_ROOT}/@CMAKE_INSTALL_RUNSTATEDIR@/modelbox
	PIDFILE=${MODELBOX_ROOT}/@CMAKE_INSTALL_RUNSTATEDIR@/modelbox/modelbox.pid
	MODELBOX_INITSCRIPT_DIR=${MODELBOX_ROOT}@CMAKE_INSTALL_FULL_SYSCONFDIR@/modelbox/init-script/modelbox
fi

MODELBOX=${MODELBOX_ROOT}@CMAKE_INSTALL_FULL_BINDIR@/modelbox

if [ ! -x "${MODELBOX}" ]; then
	echo "modelbox server not exists: ${MODELBOX}"
	exit 5
fi

case $1 in
	exec)
		if [ ! -d "$PIDDIR" ]; then
			mkdir $PIDDIR
		fi

		if [ -d ${MODELBOX_INITSCRIPT_DIR} ]; then
			for i in ${MODELBOX_INITSCRIPT_DIR}/*.sh; do
				if [ -r $i ]; then
				. $i
				fi
			done
			unset i	
		fi

		exec $MODELBOX $MODELBOX_OPTS -V -f -p $PIDFILE
		;;
	start)
		if [ ! -d "$PIDDIR" ]; then
			mkdir $PIDDIR
		fi

		if [ -d ${MODELBOX_INITSCRIPT_DIR} ]; then
			for i in ${MODELBOX_INITSCRIPT_DIR}/*.sh; do
				if [ -r $i ]; then
				. $i
				fi
			done
			unset i	
		fi

		$MODELBOX $MODELBOX_OPTS -p $PIDFILE
		LOOP=0
		while true; do
			if [ -e "$PIDFILE" ]; then
				break;
			fi
			LOOP=$((LOOP+1))

			if [ $LOOP -gt 10 ]; then
				echo "start modelbox service failed."
				"$0" stop
				exit 1
			fi
			sleep .5
		done

		PID="$(cat $PIDFILE 2>/dev/null)"
		if [ -z "$PID" ]; then
			echo "start modelbox service failed."
			exit 1
		fi
		if [ ! -e "/proc/$PID" ]; then
			echo "start modelbox service failed."
			exit 1
		fi
		echo "start modelbox service success."
		;;
	stop)
		if [ ! -f "$PIDFILE" ]; then
			echo "modelbox service is stopped."
			exit 0
		fi
		PID="$(cat $PIDFILE 2>/dev/null)"
		if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then
			echo "modelbox service is stopped"
			exit 0
		fi

		kill -TERM "$PID"
		if [ $? -ne 0 ]; then
			echo "stop modelbox service failed."
			exit 1;
		fi

		LOOP=1
		while true; do
			if [ ! -d "/proc/$PID" ]; then
				break;
			fi

			if [ $LOOP -gt 10 ]; then
				kill -9 "$PID"
				break;
			fi
			LOOP=$((LOOP+1))
			sleep .5
		done
		echo "stop modelbox service success."
		;;
	restart)
		"$0" stop && sleep 1 && "$0" start
		;;
	status)
		PID="$(cat "$PIDFILE" 2>/dev/null)"
		if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then
			echo "modelbox service is not running."
			exit 1
		fi
		echo "modelbox service is running. pid $PID"
		status=$?
		;;
	*)
		echo "Usage: $0 {start|exec|stop|restart|status}"
		exit 2
		;;
esac

exit $status

